<?php
/**************************************
 * 操作 CRM 的 RPC 接口
 *************************************/
namespace App\Handles;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

trait CrmRpcHandle
{

    /**
     * 获取CRM中RPC的接口地址
     */
    private function getCrmRpcUrl($url)
    {
        $base_url = config('rpc.crm_rpc_url');
        //dd($base_url);
        return rtrim($base_url, '/').'/'.ltrim($url, '/');
    }
    /**
     *
     * @param $params
     * @return mixed
     * @throws \Exception
     */
    public function getrpcAttrList($params)
    {
        $api = $this->getCrmRpcUrl('rpc/goods/attr_list');
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception($result['msg']);
    }




    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 5:47 下午
     * Describe: 获取banner列表
     * Version:
     * @param $source
     */
    public function getBannerList($source)
    {
        $api = $this->getCrmRpcUrl('rpc/common/banner/get_list?source='.$source);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception($result['msg']);
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:24 下午
     * Describe: IM发送消息
     * Version: V1.0
     * @param $param
     */
    public function imSendMessage($param) {
        $api = $this->getCrmRpcUrl('rpc/im/send-message');
        $http = new Client();
        $result = $http->post($api, [
            'form_params' => $param
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:26 下午
     * Describe: IM撤回消息
     * Version: V1.0
     * @param $param
     * @return mixed
     * @throws Exception
     */
    public function imWithdraw($param) {
        $api = $this->getCrmRpcUrl('rpc/im/withdraw');
        $http = new Client();
        $result = $http->post($api, [
            'form_params' => $param
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:28 下午
     * Describe: IM拉取消息
     * Version: V1.0
     * @param $param
     * @return mixed
     * @throws Exception
     */
    public function imGetSearch($param) {
        $api = $this->getCrmRpcUrl('rpc/im/search');
        $http = new Client();
        $result = $http->post($api, [
            'form_params' => $param
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }

    /**
     * 查询诊所信息
     * @throws Exception
     */
    public function getClinicHandle($clinic_id)
    {
        $api = $this->getCrmRpcUrl('rpc/clinic/info/'.$clinic_id);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询诊所失败！');
    }

    /**
     * User: Virus
     * DateTime: 2021/6/15 15:15
     * Describe: 查询诊所信息
     * Version: v1.0.0
     * @param $department_id
     * @return array
     * @throws Exception
     */
    public function getDepartmentHandle($department_id)
    {
        $api = $this->getCrmRpcUrl('rpc/clinic/department/info/'.$department_id);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询诊所失败！');
    }

    /**
     * User: Virus
     * DateTime: 2021/6/15 15:16
     * Describe: 获取医生列表
     * Version: v1.0.0
     * @param $param
     * @return array
     * @throws Exception
     */
    public function getDoctorHandle($param)
    {
        $fields = json_encode(['id', 'name', 'position', 'avatar', 'introduce', 'speciality']);
        $ids = json_encode($param['ids'], true);
        $api = $this->getCrmRpcUrl('rpc/doctor/list?ids='.$ids.'&fields='.$fields);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询医生信息失败！');
    }

    /**
     * User: Virus
     * DateTime: 2021/6/15 15:16
     * Describe: 获取医生详情
     * Version: v1.0.0
     * @param $id
     * @return array
     * @throws Exception
     */
    public function getDoctorInfoHandle($id)
    {
        $api = $this->getCrmRpcUrl('rpc/doctor/info/'.$id);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询医生信息失败！');
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:32 下午
     * Describe: 获取所有医生
     * Version: V1.0
     * @return mixed
     * @throws Exception
     */
    public function getDoctorAll() {
        $api = $this->getCrmRpcUrl('rpc/doctor/list_all');
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception($result['msg']);
    }

    /**
     * User: Virus
     * DateTime: 2021/6/16 14:00
     * Describe: 根据id获取诊所列表
     * Version: v1.0.0
     * @throws Exception
     */
    public function getClinicListHandle($param, $limit = 0)
    {
        $fields = json_encode(['id', 'name']);
        $ids = json_encode($param['ids']);
        $api = $this->getCrmRpcUrl('rpc/clinic/index/ids?ids='.$ids.'&fields='.$fields.'&limit='.$limit);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询诊所列表失败！');
    }

    /**
     * User: Virus
     * DateTime: 2021/6/16 14:00
     * Describe: 获取所有诊所列表
     * Version: v1.0.0
     * @throws Exception
     */
    public function getClinicAllHandle($param, $limit = 0)
    {
        $fields = json_encode(['id', 'name', 'legal_person', 'province', 'city', 'area', 'lng', 'lat']);
        $api = $this->getCrmRpcUrl('rpc/clinic/index?fields='.$fields.'&limit='.$limit);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询诊所列表失败！');
    }

    /**
     * User: Virus
     * DateTime: 2021/6/16 14:00
     * Describe: 根据id获取科室列表
     * Version: v1.0.0
     * @throws Exception
     */
    public function getDepartmentListHandle($param, $limit = 0)
    {
        $fields = json_encode(['id', 'name']);
        $ids = json_encode($param['ids'], true);
        $api = $this->getCrmRpcUrl('rpc/clinic/department?ids='.$ids.'&fields='.$fields.'&limit='.$limit);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询诊所列表失败！');
    }

    /**
     * User: Virus
     * DateTime: 2021/6/16 14:00
     * Describe: 获取所有科室列表
     * Version: v1.0.0
     * @throws Exception
     */
    public function getDepartmentAllHandle($param, $limit = 0)
    {
        $fields = json_encode(['id', 'name']);
        $c_id = $param['c_id'] ?? 0;
        $api = $this->getCrmRpcUrl('rpc/clinic/department/list?c_id='.$c_id.'&fields='.$fields.'&limit='.$limit);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询诊所列表失败！');
    }

    /**
     * User: Virus
     * DateTime: 2021/6/21 15:06
     * Describe: 获取用户详情
     * Version: v1.0.0
     * @param $id
     * @return mixed
     * @throws Exception
     */
    public function getUserInfoHandle($id)
    {
        $api = $this->getCrmRpcUrl('rpc/user/info?condition=' . $id);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询诊所列表失败！');
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:43 下午
     * Describe: 更换用户手机号
     * Version: V1.0
     * @param $phone
     * @param $code
     * @param $userId
     * @return mixed
     * @throws Exception
     */
    public function userChangePhone($phone, $userId) {
        $api = $this->getCrmRpcUrl('rpc/user/update/phone');
        $http = new Client();
        $result = $http->post($api, [
            'form_params' => [
                'phone' => $phone,
                'u_id' => $userId
            ]
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:52 下午
     * Describe: 通知用户认证信息
     * Version: V1.0
     * @param $realName
     * @param $idCard
     * @param $userId
     */
    public function userUpdateAuth($realName, $idCard, $userId) {
        $api = $this->getCrmRpcUrl('rpc/user/update/auth');
        $http = new Client();
        $result = $http->post($api, [
            'form_params' => [
                'real_name' => $realName,
                'id_card' => $idCard,
                'id' => $userId
            ]
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:36 下午
     * Describe: 医生根据手机号登录
     * Version: V1.0
     * @param $phone
     * @param $code
     * @return mixed
     * @throws Exception
     */
    public function doctorLoginByPhone($phone) {
        $api = $this->getCrmRpcUrl('rpc/doctor/login/phone');
        $http = new Client();
        $result = $http->post($api, [
            'form_params' => [
                'phone' => $phone
            ]
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:38 下午
     * Describe: 医生根据手机号登录
     * Version: V1.0
     * @param $phone
     */
    public function userLoginByPhone($phone) {
        $api = $this->getCrmRpcUrl('rpc/user/login/phone');
        $http = new Client();

        $result = $http->post($api, [
            'form_params' => [
                'phone' => $phone
            ]
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }


    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/22 6:49 下午
     * Describe: 查询是否存在认证数据，不存在调用身份认证接口
     * Version: V1.0
     * @param $realName
     * @param $idCard
     * @param $identityType
     */
    public function userAuthCreate($realName, $idCard, $identityType) {
        $api = $this->getCrmRpcUrl('rpc/user/auth/create');
        $http = new Client();
        $result = $http->post($api, [
            'form_params' => [
                'real_name' => $realName,
                'id_card' => $idCard,
                'identity_type' => $identityType
            ]
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }



    /**
     * User: Virus
     * DateTime: 2021/6/21 15:06
     * Describe: 获取用户详情
     * Version: v1.0.0
     * @param $id
     * @return mixed
     * @throws Exception
     */
    public function userAuth($id)
    {
        $api = $this->getCrmRpcUrl('rpc/user/im/user_auth?id=' . $id);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, JSON_OBJECT_AS_ARRAY);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询用户导入状态失败！');
    }

    /**
     * User:    孟郝泽
     * DateTime: 2021/6/22 6:49 下午
     * Describe: im 查询用户
     * Version: V1.0
     * @param $id
     */
        public function userAuthAdd($id) {
            $api = $this->getCrmRpcUrl('rpc/user/im/useradd');
            $http = new Client();
            $result = $http->post($api, [
                'form_params' => [
                    'id' => $id,
                ]
            ]);
            $result = (String)$result->getBody();
            $result = json_decode($result, true);
            if($result['code'] == 200){
                return $result['data'];
            }

            throw new Exception($result['msg']);
        }


    /**
     * User: Virus
     * DateTime: 2021/6/21 15:06
     * Describe: 获取医生用户详情
     * Version: v1.0.0
     * @param $id
     * @return mixed
     * @throws Exception
     */
    public function doctorAuth($id)
    {
        $api = $this->getCrmRpcUrl('rpc/doctor/im/doctor_auth?id=' . $id);
        $http = new Client();
        $result = $http->get($api);
        $result = (String)$result->getBody();
        $result = json_decode($result, JSON_OBJECT_AS_ARRAY);
        if($result['code'] == 200){
            return $result['data'];
        }
        throw new Exception('查询用户导入状态失败！');
    }

    /**
     * User:    孟郝泽
     * DateTime: 2021/6/22 6:49 下午
     * Describe: im 查询用户
     * Version: V1.0
     * @param $id
     */
    public function doctorAuthAdd($id) {
        $api = $this->getCrmRpcUrl('rpc/doctor/im/doctor_auth_add');
        $http = new Client();
        $result = $http->post($api, [
            'form_params' => [
                'id' => $id,
            ]
        ]);
        $result = (String)$result->getBody();
        $result = json_decode($result, true);
        if($result['code'] == 200){
            return $result['data'];
        }

        throw new Exception($result['msg']);
    }
}
